package com.jianganming.flyweight;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//享元池，或者享元工厂（Spring的IOC本质就是享元）
public class TrainTicketPool {
    private Map<String,TrainTicket> pool = new ConcurrentHashMap<>();

    public TrainTicket queryTicket(String from,String to){
        String key = from+"->"+to;
        if(pool.containsKey(key)){
            return pool.get(key);
        }
        TrainTicket trainTicket = new TrainTicket(from,to);
        pool.put(key,trainTicket);
        return trainTicket;
    }

    public void showAll(){
        for (Map.Entry<String, TrainTicket> entry : pool.entrySet()) {
            System.out.println(entry.getKey());
        }
    }
}
